﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.BLL;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.Common;
using System.Collections.Generic;
using System.Linq;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Manage_Project : System.Web.UI.Page
    {
        private ComboBLL comboBLL = new ComboBLL();
        private ManageProject projectBLL = new ManageProject();
        int year = SystemInfo._CurrentYear;
        private SystemControlBLL scBLL = new SystemControlBLL();
        private OptionTypeBLL optionTypeBLL = new OptionTypeBLL();
        Main main = new Main();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtBgmYear.Text = SystemInfo._CurrentYearReq.ToString();
                LoadJobSiteCombo();
                LoadRptViewOptionData();

                //================ test ======================
                if (Request.QueryString["jid"] != null)
                {
                    ddlJobSite.SelectedValue = Request.QueryString["jid"];
                }
                //============================================

            }
        }

        private void LoadData()
        {
            string strYear = txtBgmYear.Text;
            string strJobsiteSeq = ddlJobSite.SelectedValue;
            string strBgmProjSeq = ddlBgmProjSeq.SelectedValue;

            tvlProject.DataSource = projectBLL.GetProjectReportOnDemand(ManageProject.ProjectDataType.Project
                , strYear, strJobsiteSeq, strBgmProjSeq, null);
            //tvlProject.DataSource = projectBLL.GetProjectReport(strYear, strJobsiteSeq, strBgmProjSeq);
            tvlProject.DataBind();
        }

        private void LoadJobSiteCombo()
        {
            string jobSiteID = (string)new SsMng().GetUss(SsMng.SsUserType.WorkingJobSiteSeq);
            string viewOnly = Request.QueryString["type"];
            DataTable dt = comboBLL.ListJobsite();
            ddlJobSite.DataSource = dt;
            ddlJobSite.DataBind();

            main.InsertEmptyDDLAll(ddlJobSite);

            ddlJobSite.SelectedValue = jobSiteID;
            if ((jobSiteID != scBLL.JobSiteStrategyID() && jobSiteID != scBLL.JobSiteFinanceID() && jobSiteID != "127") && jobSiteID != "")
            {
                ddlJobSite.Enabled = false;
            }
            else
            {
                ddlJobSite.Enabled = true;
            }

            LoadProjCombo();
        }

        private void LoadProjCombo()
        {
            string jobSiteID = ddlJobSite.SelectedValue;
            DataTable dt = comboBLL.ListProject(txtBgmYear.Text, ddlJobSite.SelectedValue, null);
            ddlBgmProjSeq.DataSource = dt;
            ddlBgmProjSeq.DataBind();

            ddlBgmProjSeq.Items.Insert(0, new RadComboBoxItem("----- โครงการทั้งหมด -----", ""));
            ddlBgmProjSeq.SelectedIndex = 0;
        }

        private void LoadRptViewOptionData()
        {
            IList<OptionTypeInfo> optionType = optionTypeBLL.Search(-1);

            rptViewOptionData.DataSource = optionType;
            rptViewOptionData.DataBind();
        }

        protected void ddlJobSite_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadProjCombo();
            LoadData();
        }

        protected void ddlYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadProjCombo();
            LoadData();
        }
        
        protected void ddlBgmProjSeq_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadData();
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadProjCombo();
            LoadData();
        }

        protected void tvlProject_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
        {
            if (e.Item.ItemType == TreeListItemType.Item || e.Item.ItemType == TreeListItemType.AlternatingItem)
            {
                TreeListDataItem item = (TreeListDataItem)e.Item;
                string strParentSeq = item["child_seq"].Text;
                bool bRoot = (strParentSeq.Length - strParentSeq.Replace("/", "").Length) == 1;

                ((Label)item["name"].FindControl("lblName")).Visible = !bRoot;
                ((LinkButton)item["name"].FindControl("cmdViewOperation")).Visible = bRoot;
                //((ImageButton)item["name"].FindControl("ibmExpandAll")).Visible = bRoot;

                if (bRoot)
                {
                    item.ForeColor = System.Drawing.Color.Blue;
                }
                else
                {
                    item.ForeColor = System.Drawing.Color.Black;
                }
            }
        }

        protected void tvlProject_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            string strYear = txtBgmYear.Text;
            string strJobsiteSeq = ddlJobSite.SelectedValue;
            string strBgmProjSeq = ddlBgmProjSeq.SelectedValue;

            //var models = projectBLL.GetProjectReportViewState(strYear, strJobsiteSeq, strBgmProjSeq);
            //ViewState["ProjectReport"] = models;

            //IList<ViewReportModel> sumModels = (from a in models
            //                                    group a by new { a.bgm_proj_code, a.bgm_proj_name } into g
            //                                    select new ViewReportModel
            //                                    {
            //                                        code = g.Key.bgm_proj_code,
            //                                        name = g.Key.bgm_proj_name,
            //                                        icm_amount = g.Sum(o => o.icm_amount),
            //                                        money_req_amount = g.Sum(o => o.money_req_amount),
            //                                        money_return_amount = g.Sum(o => o.money_return_amount),
            //                                        operation_amount = g.Sum(o => o.operation_amount),
            //                                        operation_return_amount = g.Sum(o => o.operation_return_amount),
            //                                        pay_amount = g.Sum(o => o.pay_amount),
            //                                        project_amount = g.Sum(o => o.project_amount),
            //                                        transfer_amount = g.Sum(o => o.transfer_amount),
            //                                        type = ManageProject.ProjectDataType.Project,
            //                                    }).ToList();

            //tvlProject.DataSource = sumModels;//models.Sum(o => { o.icm_amount, o.money_req_amount, o.money_return_amount, o.operation_amount, o.operation_return_amount, o.pay_amount, o.project_amount, o.transfer_amount });
            tvlProject.DataSource = projectBLL.GetProjectReportOnDemand(ManageProject.ProjectDataType.Project
                , strYear, strJobsiteSeq, strBgmProjSeq, null);
        }

        protected void tvlProject_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == "ExpandAll")
            {
                ExpandAll((TreeListDataItem)e.Item);
            }
        }

        private void ExpandAll(TreeListDataItem _item)
        {
            if (_item.CanExpand)
            {
                _item.Expanded = true;
            }

            foreach (TreeListDataItem item in _item.ChildItems)
            {
                ExpandAll(item);
            }
        }

        protected void tvlProject_ChildItemsDataBind(object sender, TreeListChildItemsDataBindEventArgs e)
        {
            string strParentSeq = e.ParentDataKeyValues["child_seq"].ToString();
            int count = strParentSeq.Length - strParentSeq.Replace("/", "").Length;
            ManageProject.ProjectDataType type;

            switch (count)
            {
                case 1:
                    type = ManageProject.ProjectDataType.Source;
                    break;
                //case 2:
                //    type = ManageProject.ProjectDataType.ExpenseGroup;
                //    break;
                //case 3:
                //    type = ManageProject.ProjectDataType.Expense1;
                //    break;
                //case 4:
                //    type = ManageProject.ProjectDataType.Expense2;
                //    break;
                //case 5:
                //    type = ManageProject.ProjectDataType.Expense3;
                //    break;
                default:
                    return;
                    //type = ManageProject.ProjectDataType.Source;
                    break;
            }

            e.ChildItemsDataSource = projectBLL.GetProjectReportOnDemand(type, null, null, null, strParentSeq);
        }

        protected void ddlFilterBgmOptionSeq_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }
    }
}
